﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DeviceInfoManage.DataAccessLayer;


namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    ///DutyArrange 的摘要说明
    /// </summary>
    [Serializable]
    public class DutyArrange
    {
        #region 字段属性
        private int _id;
        private string _person;
        private DateTime _date;
        private string _description;
        private string _response;
        private string _phonenumber;
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 值班人员
        /// </summary>
        public string Person
        {
            set { _person = value; }
            get { return _person; }
        }
        /// <summary>
        /// 值班日期 
        /// </summary>
        public DateTime Date
        {
            set { _date = value; }
            get { return _date; }
        }
        /// <summary>
        /// 任务描述
        /// </summary>
        public string Description
        {
            set { _description = value; }
            get { return _description; }
        }
        /// <summary>
        /// 任务反馈
        /// </summary>
        public string Response
        {
            set { _response = value; }
            get { return _response; }
        }
        /// <summary>
        /// 联系电话
        /// </summary>
        public string PhoneNumber
        {
            get { return _phonenumber; }
            set { _phonenumber = value; }
        }
        #endregion

        #region 构造函数
        public DutyArrange()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "DutyArrange");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DutyArrange");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DutyArrange model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DutyArrange(");
            strSql.Append("Person,Date,Description,Response)");
            strSql.Append(" values (");
            strSql.Append("@Person,@Date,@Description,@Response)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Person", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@Description", SqlDbType.VarChar,1000),
					new SqlParameter("@Response", SqlDbType.VarChar,1000)};
            parameters[0].Value = model.Person;
            parameters[1].Value = model.Date;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.Response;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(DutyArrange model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DutyArrange set ");
            strSql.Append("Person=@Person,");
            strSql.Append("Date=@Date,");
            strSql.Append("Description=@Description,");
            strSql.Append("Response=@Response");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@Person", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@Description", SqlDbType.VarChar,1000),
					new SqlParameter("@Response", SqlDbType.VarChar,1000)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Person;
            parameters[2].Value = model.Date;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.Response;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DutyArrange ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DutyArrange GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Person,Date,Description,Response from DutyArrange ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DutyArrange model = new DutyArrange();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.Person = ds.Tables[0].Rows[0]["Person"].ToString();
                if (ds.Tables[0].Rows[0]["Date"].ToString() != "")
                {
                    model.Date = DateTime.Parse(ds.Tables[0].Rows[0]["Date"].ToString());
                }
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Response = ds.Tables[0].Rows[0]["Response"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DutyArrange GetModelFromView(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Person,Date,Description,Response,PhoneNumber from DutyView ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DutyArrange model = new DutyArrange();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.Person = ds.Tables[0].Rows[0]["Person"].ToString();
                if (ds.Tables[0].Rows[0]["Date"].ToString() != "")
                {
                    model.Date = DateTime.Parse(ds.Tables[0].Rows[0]["Date"].ToString());
                }
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Response = ds.Tables[0].Rows[0]["Response"].ToString();
                model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PhoneNumber,Person,Date,Description ");
            strSql.Append(" FROM DutyView ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Person,Date,Description,Response ");
            strSql.Append(" FROM DutyArrange ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}